﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Entities;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class AdministratorLoginDAO : Repository<Model.Entities.Model_Administrator>, IAdministratorDAO
    {
        public override IList<Model.Entities.Model_Administrator> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }

        public override Model_Administrator GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Administrators.FirstOrDefault(p => p.ID == id && p.IsUse == true && p.IsDelete == false);
            }
        }

        public override bool Exists(string id)
        {
            Model_Administrator administrator = GetByID(id);
            return administrator != null ? true : false;
        }

        public override bool Add(Model_Administrator entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }

        public override bool Update(Model_Administrator entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }

        public override bool Delete(Model_Administrator entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }

        public override bool Delete(string id)
        {
            Model.Entities.Model_Administrator administrator = GetByID(id);
            return Delete(administrator);
        }

        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from p in context.Administrators
                                  where listID.Contains(p.ID)
                                  select p;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_Administrator>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                }
                return context.SaveChanges();
            }
        }
        public Model.Entities.Model_Administrator GetByName(string name)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Administrators.FirstOrDefault(a => a.LoginName.Equals(name, StringComparison.CurrentCultureIgnoreCase) && a.IsUse == true && a.IsDelete == false);
            }
        }


        public Model_Administrator GetByNameAndPassword(string name, string password)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Administrators.FirstOrDefault(a => a.LoginName.Equals(name, StringComparison.CurrentCultureIgnoreCase) && a.LoginPassword.Equals(password, StringComparison.CurrentCultureIgnoreCase)&&a.IsUse==true&&a.IsDelete==false);
            }
        }


        public List<Model_Administrator> GetAllAdministrators()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.Administrators.Where(t => t.IsUse == true && t.IsDelete == false).ToList();
            }
        }
    }
}
